package com.jt.demo1;
import com.jt.controller.User;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequestMapping("/user")
@RestController   //==@Controller+@ResponseBody
public class UserController{
    /**
     * spring底层实现
     * URL:http://localhost:8080//user/findUser?id=100&name=贝仁杰2b
     * 类型：get请求
     * 返回值：String id：name
     * HttpServletRequest    请求数据
     * HttpServletResponse   响应数据
     */
    @RequestMapping("/findUser1")
    @ResponseBody                       //响应数据 JSON
    public String findUser(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        String name = request.getParameter("name");
        return i+":"+name;
    }


    /**
     * URL:http://localhost:8080//user/findUser2?id=100&name=贝仁杰2b
     * 规则：springMVC中参数名称，与传递参数名称一致
     * 原理：mvc将Servlet的取值方式包装，只需填写参数，即获取数据
     *
     * @return
     */

    @RequestMapping("/findUser2")
    @ResponseBody                       //响应数据 JSON
    public String findUser2(Integer id,String name){
        return id+":"+name;
    }

    /**
     * URL:
     *返回值 id;name;age
     *
     * @returnhttp:
     * url；localhost:8080//user/findUser3?id=100&name=贝仁杰2b&age=12&贝仁杰=sb
     */
    @RequestMapping("/findUser3")
    @ResponseBody                       //响应数据 JSON
    public User findUser3(User user){
        return user;
    }
    /**
     * RestFul 结构 动态接受参数
     *url；localhost:8080/user/restFul2/1/张三
     */

    @RequestMapping("/restFul2/{id}/{name}")
    public User restful(@PathVariable Integer id,
                        @PathVariable String name){
        User user = new User();
        user.setId(id);
        user.setName(name);
        return  user;
    }


    /**
     *    报错：405  匹配类型不匹配
     *    查询  @GetMapping
     *    新增  @PostMapping
     *      @PutMapping
     *      @DeleteMapping
     */
    @RequestMapping("/restFul2/{id}/{name}/{age}{ss}")
    @GetMapping("/restFul2/{id}/{name}/{age}{ss}")
    @PostMapping("/restFul2/{id}/{name}/{age}{ss}")
    @PutMapping("/restFul2/{id}/{name}/{age}{ss}")
    @DeleteMapping("/restFul2/{id}/{name}/{age}{ss}")
    public User restful2(User user){

        return  user;
    }

    /**
     * URL:http://localhost:8080/user/saver?name=12,你,好
     * @param nama
     * @return
     */
    //1.public String  check(String[] name){
    @GetMapping("/saver")    //网页get请求
    public String[]  check(String[] name){
        //1.return Arrays.toString(name);
        return name;
    }
}
